function TimesheetAssistant(perioditem) {
    this.perioditem = perioditem;
	this.sheetId = this.perioditem.SheetId;
	//this.sheetId = '2433710';
};

TimesheetAssistant.prototype.setup = function() {
this.debugContainer = this.controller.get("debugOutput");
    this.logOutputNum = 0;	
	this.sheetValues = [];
	this.sheetItems = getLocalData("sheet_"+this.sheetId)||[];
    //init day list
	this.ListDayModel = {
		value : "Mon"
	};
	this.CurrentDay = "Mon";
	var TitleChoices = [{label :"Sun",value :"Sun"},
	                    {label :"Mon",value :"Mon"},
						{label :"Tue",value :"Tue"},
						{label :"Wed",value :"Wed"},
						{label :"Thu",value :"Thu"},
						{label :"Fri",value :"Fri"},
						{label :"Sat",value :"Sat"}
						];
	this.controller.setupWidget("list_Day", {
		choices : TitleChoices
	}, this.ListDayModel);
	Mojo.Event.listen(this.controller.get("list_Day"),
                      Mojo.Event.propertyChange, this.listdaychange.bind(this));
	// set up week total
	var total = 0;
	for(var i=0;i<this.sheetItems.length;i++){
	    for(var j=0; j<7; j++){
		    total +=parseFloat(this.sheetItems[i][weekday[j]]);
		}
	};
	
	this.totalweekModel = {
		value : total,
		disabled: true
	};
	//this.controller.setupWidget("txtweekhours",{enterSubmits: false}, this.totalweekModel});
	this.controller.setupWidget("txtweekhours",
	  this.attributes = {
		  multiline: false,
		  enterSubmits: false,
		  focus: false
	  },
	  this.totalweekModel
	);
	// set up buttons	
	this.controller.setupWidget("btnSave", { type: Mojo.Widget.activityButton },
		    {
			label : "Save",
			buttonClass : "buttonfloat affirmative",
		    }
		  ); 
	Mojo.Event.listen(this.controller.get("btnSave"), Mojo.Event.tap, 
				    this.btnSaveClick.bind(this)
				  ); 
	this.controller.setupWidget("btnSubmit", { type: Mojo.Widget.activityButton },
		    {
			label : "Submit",
			disabled: true,
			buttonClass : "buttonfloat affirmative",
		    }
		  ); 
	Mojo.Event.listen(this.controller.get("btnSubmit"), Mojo.Event.tap, 
				    this.callGetTimesheet.bind(this)
				  ); 
	this.controller.setupWidget("btnReset", { type: Mojo.Widget.activityButton },
		    {
			label : "Reset",
			buttonClass : "buttonfloat affirmative",
			disabled:!islogin,
		    }
		  ); 
	Mojo.Event.listen(this.controller.get("btnReset"), Mojo.Event.tap, 
				    this.callGetTimesheet.bind(this)
				  ); 
	//init time sheet list
	this.timelistModel = {listTitle: username, items:this.sheetItems||[]};
		
	this.controller.setupWidget('listTimeSheets', 
				    {itemTemplate:'timesheet/listitem',listTemplate:'timesheet/listcontainer'},
				     this.timelistModel);
	//Mojo.Event.listen(this.controller.get("listTimeSheets"), Mojo.Event.listAdd, this.sheetlistAdd.bind(this));


	Mojo.Event.listen(this.controller.get("listTimeSheets"), Mojo.Event.listChange,
                    this.sheetlistChange.bind(this));
};

TimesheetAssistant.prototype.activate = function(event) {
	if(!getLocalData("sheet_"+this.sheetId)){
		this.callGetTimesheet();
   }
};

TimesheetAssistant.prototype.listdaychange = function(event) {
	    var m = 0;
		while(m < this.sheetItems.length ) {
			if(this.sheetItems[m]){
				this.sheetItems[m][this.CurrentDay] = this.timelistModel.items[m].Current;
				this.sheetItems[m].Current=parseFloat(this.sheetItems[m][this.ListDayModel.value]);
			}
			m++;
		};
		var total = 0;
		for(var i=0;i<this.sheetItems.length;i++){
			for(var j=0; j<7; j++){
				total +=parseFloat(this.timelistModel.items[i][weekday[j]]);
			}
		};
		this.totalweekModel.value = total;
		this.controller.modelChanged(this.totalweekModel);
		this.CurrentDay = this.ListDayModel.value;
		this.timelistModel.items = this.sheetItems;
		this.controller.modelChanged(this.timelistModel);
};
TimesheetAssistant.prototype.btnSaveClick = function(event) {
    setLocalData("sheet_"+this.sheetId,this.sheetItems);	
	this.controller.get("btnSave").mojo.deactivate();    
};
TimesheetAssistant.prototype.sheetlistChange = function(event) {
	if(event.originalEvent.target.tagName == "INPUT") {
	    event.item.Current = event.originalEvent.target.value;
		
	}
};
TimesheetAssistant.prototype.callGetTimesheet = function(event) {
    var f = new Future();
    //Mojo.Log.info("configObject before trans " + JSON.stringify(configObject));

    var configObj = configObject;

	callTransService(configObj, 'getPPMTimesheetbyID', {"timesheetId":this.sheetId}, f);

    f.then( function(future) {
        //this.logMsg( "code : " + future.result.code +", Service Return: " +JSON.stringify(future.result.reply));     
        Mojo.Log.info(" future result ", JSON.stringify(future.result));
		this.sheetValues = future.result.reply;
		this.sheetItems = [];
		for(var i = 0 ;i < this.sheetValues.length;i++ ) {
			if(this.sheetValues[i].task_id_1){
				var item = {};
				for(var d = 0; d<weekday.length; d++){
					item[weekday[d]] = this.sheetValues[i][weekday[d]];
				}
				item["Current"]= parseFloat(this.sheetValues[i][this.ListDayModel.value])||"0.0";
				for(var j = 0 ;j < this.sheetValues.length;j++){
				    var itemid = this.sheetValues[j].TaskID;
				    if(itemid && itemid.indexOf("&nbsp;")<0){						
						if(itemid.indexOf(this.sheetValues[i].task_id_2)>0 && itemid.indexOf(this.sheetValues[i].task_id_3)>0){
							item["SheetItem"]= this.sheetValues[j].Item;
							item["Status"]= this.sheetValues[j].Status;
							item["Activity"]= this.sheetValues[j].Activity;							
						}
					}
				}
				this.sheetItems.push(item);
			}
		};
		this.timelistModel.items = this.sheetItems;
		setLocalData("sheet_"+this.sheetId,this.sheetItems);
		this.controller.modelChanged(this.timelistModel);
		this.controller.get("btnReset").mojo.deactivate();
		//this.logMsg("timelistModel: "+JSON.stringify(this.timelistModel));
    }.bind(this));

};
TimesheetAssistant.prototype.logMsg = function(logText) {
    //this.debugContainer.innerHTML = "" + (this.logOutputNum++) + ": " + StringUtils.escapeHTML(logText) + "<br/>" + this.debugContainer.innerHTML;
    //this.debugContainer.innerHTML = (this.logOutputNum++) + ": " + logText + "" + this.debugContainer.innerHTML;            
}; 